#include "readfile.h"

std::map<std::string,std::map<std::string,std::string> > mapconf;

void readFile()
{
   std::fstream file;
   char buf[BUFLEN] = {0};
   std::string readstr = "";
   std::string keyname,valname,mdname;
   file.open("/etc/dameon.conf", std::ios::in|std::ios::binary);
   
   if(!file)
	   return;   
	    
   readstr.clear();
   keyname.clear();
   valname.clear();
   mdname.clear();
   
   while(file.getline(buf,BUFLEN)) {
		readstr = buf;
		int n =0;
		if(readstr[0] == ';')
			continue;
		else if(readstr[0] == '[')
		{
			int nkey = readstr.find("]");
			mdname = readstr.substr(1,nkey-1);
			continue;
		}
		else
		{
			std::string tempstr = "";
			for(int i =0;i<BUFLEN;i++)
			{				
				if(readstr[i]==' ')
					continue;
				else
				{
					if(readstr[i] == '\000')		
						break;
					tempstr+=readstr[i];
				}
			}
			int length = tempstr.length();
			int ncout = tempstr.find("=");
			keyname = tempstr.substr(0,ncout);
			valname = tempstr.substr(ncout+1,length-ncout-1);
			mapconf[mdname][keyname] =valname;	
		}
   }
	file.close();
}

std::string get_config(std::string modulename,std::string param)
{
    return mapconf[modulename][param];
}


